﻿using System;
using System.Xml;
using System.IO;
using System.Text;
using System.ComponentModel;
using System.Configuration;
using System.Configuration.Install;
using System.Diagnostics;


namespace Forex.IB.RealTimeBars.WinApp
{
    [RunInstaller(true)]
    public class InstallHelper : Installer
    {
        public InstallHelper()
        {

        }

        public override void Install(System.Collections.IDictionary stateSaver)
        {

            base.Install(stateSaver);

            string installDir = new FileInfo(Context.Parameters["AssemblyPath"]).DirectoryName;
            string dataSource = Context.Parameters["DataSource"];
            string initialCatalog = Context.Parameters["InitialCatalog"];
            string toList = Context.Parameters["To"];
            string configFile = string.Empty;

            DirectoryInfo di = new DirectoryInfo(installDir);

            // Figure out what other entries are in that directory.
            FileSystemInfo[] fsi = di.GetFileSystemInfos();

            // Print the names of all the files and subdirectories of that directory.
            foreach (FileSystemInfo info in fsi)
            {
                if (info.FullName.Contains(".exe"))
                {
                    configFile = info.FullName;
                    break;
                }
            }

            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(configFile);
            config.AppSettings.Settings["LogFile"].Value = installDir+"\\";
            config.AppSettings.Settings["DataSource"].Value = dataSource;
            config.AppSettings.Settings["InitialCatalog"].Value = initialCatalog;
            config.AppSettings.Settings["To"].Value = toList;
            config.Save();

        }
    }
}
